package basicservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type DictionaryMainAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDictionaryMainAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DictionaryMainAddLogic {
	return &DictionaryMainAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// DictionaryMainAdd 创建字典主表
func (l *DictionaryMainAddLogic) DictionaryMainAdd(in *property_service.DictionaryMain) (*property_service.Response, error) {
	// 获取字典主表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).DictionaryMain
	// 创建一个新的字典主表模型对象，不包含ID、UpdatedAt、CreatedAt字段
	mod := &model.DictionaryMain{
		DictionaryName: in.DictionaryName, // 字典名称
		DictKey:        in.DictKey,        // 字段唯一key
		Code:           in.Code,           // 字典编码
		Description:    in.Description,    // 字典描述
		CreatedAtuser:  in.CreatedAtuser,  // 创建人ID
		UpdatedAtuser:  in.UpdatedAtuser,  // 修改人ID
		TenantID:       in.TenantID,       // 租户ID
	}

	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		// 如果创建失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 创建成功，返回成功响应
	return result.Suc().AddEd()
}
